<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\SendTemplate as sendTemplateModel;
use think\facade\Db;
use think\facade\View;

class SendTemplate extends Base{
    //用分页读取数据
    public function index(){
        $signid = input('signid'); $temp_type = input('temp_type');
        $keyword = input('keyword'); $order = ['id'=>'desc'];

        // 赋值状态
        if(!is_numeric($signid)) $signid = 1;
        View::assign('signid',$signid);

        $rs = sendTemplateModel::index($signid,$this->lang,$temp_type,$keyword,$order);

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }

    /*新增编辑
    * 返回json值：success成功，error失败，active_time:error有效时间错误
    */
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = sendTemplateModel::find($id);
            View::assign('rs',$rs);

            //模板名称属性
            $attributeWhere[] = ['tabledir','=','send_template']; $attributeWhere[] = ['lang','=',$this->lang];
            View::assign('attribute',Db::name('attribute')->where($attributeWhere)->order('sequence desc')->select());

            return view();
        }else{
            //有效时间错误
            if(!preg_match("/^[1-9]\d*$/",input('active_time'))){
                return json(['code' => 1001, 'msg' => lang('v_active').lang('v_time'), 'data' => '']);
            }
            //内容错误
            if(empty(input('content'))){
                return json(['code' => 1002, 'msg' => lang('c_error_parameter'), 'data' => '']);
            }
            //短信模板ID错误
            if(input('temp_type') == 'sms' and empty(input('tempid'))){
                return json(['code' => 1003, 'msg' => lang('c_error_parameter'), 'data' => '']);
            }
            //邮件选项错误
            if(input('temp_type') == 'email' and (empty(input('temp_title')) or empty(input('send_name')) or empty(input('reply_address')))){
                return json(['code' => 1004, 'msg' => lang('c_error_parameter'), 'data' => '']);
            }

            try {
                sendTemplateModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }       
    }

    // 单个更新状态
    public function update_signid(){
        $id = input('id'); $signid = input('signid');
        // 参数不能为空
        if(empty($id) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        // 软删除
        if($signid == 9) $signid = time();
        
        try {
            Db::name('send_template')->where('id',$id)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 批量更新状态
    public function updateall_signid(){
        $selectid = input('selectid/a'); $signid = input('signid');
        // 参数错误
        if(empty($selectid) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();
        
        try {
            Db::name('send_template')->where('id','in',$selectid)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

	/*单条删除
    * 返回json值：success成功，error失败，delete:error删除失败
    */
    public function delete_one(){
        try {
            $id = input('id');
            $sendTemplate = sendTemplateModel::find($id);

            $sendTemplate->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
    /*批量删除
    * 返回json值：success成功，error失败，select:error没有选择数据，delete:error删除失败
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }

        try {
            Db::name('send_template')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}